package sim.gui;

import java.awt.*;
import javax.swing.*;

import sim.core.*;
import sim.util.*;

import java.awt.event.*;

@SuppressWarnings("serial")
public class Gui extends javax.swing.JFrame {

	public static Gui Gui ;
	private Panel panel;
	
	public Gui() {
		super();
		initGUI();
		Gui = this ;
	}

	public void addItem( Displayable d ) {
		panel.items.add( d ) ;
	}
	
	public void removeItem( Displayable d ) {
		panel.items.remove( d ) ;
	}
	
	public XY getMouseXY() {
		return panel.mouse() ;
	}
	
	public void start() {
		setVisible(true);
		new Thread() {
			public void start() {
				setDaemon( true ) ;
				super.start() ;
			}
			
			public void run() {
				for(;;)
					try {
						panel.repaint() ;						 
						Thread.sleep(66) ;
					} catch( Exception x ) {}
			}
        }.start() ;
	}
	
	private void initGUI() {
		try {
			{
				this.setFocusable(false);
				this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			}
			{
				getContentPane().setBackground(new java.awt.Color(255,255,255));
				this.addWindowListener(new WindowAdapter() {
					public void windowClosed(WindowEvent evt) {
						System.out.println("this.windowClosed, event=" + evt);
					}
				});
			}
			{
				panel = new Panel();
				getContentPane().add(panel, BorderLayout.CENTER);
				panel.setBackground(new java.awt.Color(255,255,255));
				panel.addMouseListener(new MouseAdapter() {
					public void mouseExited(MouseEvent evt) {
						panel.setMousePosition( Integer.MIN_VALUE, Integer.MIN_VALUE ) ;
						panel.repaint() ;
					}
				});
				panel.addMouseMotionListener(new MouseMotionAdapter() {
					public void mouseDragged(MouseEvent evt) {	
						panel.setMousePosition( evt.getX(), evt.getY() ) ;
						panel.repaint() ;
					}

					public void mouseMoved(MouseEvent evt) {
						panel.setMousePosition( evt.getX(), evt.getY() ) ;
						panel.repaint();
					}
				});		
			}
			this.setSize(800, 600);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
